package cn.tedu.day01.mp.controller;

import cn.tedu.day01.mp.pojo.User;
import cn.tedu.day01.mp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public List<User> queryAllUsers(){
        return userService.queryAll();
    }
    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        int i = userService.addUser(user);
        if(i>0){
            return "添加成功！";
        }
        return "添加失败";
    }
    @RequestMapping("/deleteById")
    public String deleteUserById( String id){
        int i = userService.deleteUser(Integer.parseInt(id));
        if (i>0){
            return "删除成功！";
        }
        return "删除失败 !";
    }

    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user){
       int row =  userService.updateUser(user);
       if (row>0){
           return "修改成功";
       }
       return  "修改失败";
    }

}
